package com.alinks.modbus.slave.codec;

import com.alinks.modbus.core.codec.tcp.TcpModbusCodec;
import com.alinks.modbus.core.frame.ModbusFrame;
import com.alinks.modbus.core.frame.TcpFrame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;

import java.util.List;


public class TcpMessageCodec extends ByteToMessageCodec<ModbusFrame> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ModbusFrame modbusFrame, ByteBuf byteBuf) throws Exception {
        TcpModbusCodec tcpModbusCodec = new TcpModbusCodec();
        tcpModbusCodec.encode((TcpFrame.Response)modbusFrame, byteBuf);
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        TcpModbusCodec tcpModbusCodec = new TcpModbusCodec();
        ModbusFrame frame = tcpModbusCodec.decode(byteBuf);
        list.add(frame);
    }
}
